package org.itstack.demo.design;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;

/**
 * @Author: fanYuLe
 * @CreateTime: 2023-12-27  10:41
 * @Description:
 */
public class JDBCUtil {
    private static Logger logger = LoggerFactory.getLogger(JDBCUtil.class);

    public static final String URL = "jdbc:mysql://127.0.0.1:3306/sqlexercise";
    public static final String USER ="root";
    public static final String PASSWORD = "root";

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.加载驱动程序
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获得数据库连接
        Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
        //3.操作数据库，实现增删改查

        Statement stmt = conn.createStatement();
        ResultSet resultSet = stmt.executeQuery("select s_id,s_name from student");
        //如果有数据，rs.next()返回true
        while (resultSet.next()){
            logger.info("测试结果 id：{} 姓名：{}",resultSet.getString("s_id"),resultSet.getString("s_name"));
        }
    }
}
